var router = require('koa-router')();

import config from '../config';
// -----------财务公司相关接口Start-----------------
//财务公司列表
router.post('/financeCompany/list',async (ctx, next)=>{
    let {
    	pageNumber=1,
    	pageSize=10,
    	keyword=""
    } = ctx.request.body;


    let result = await ctx.get('/web/company/list',{
			    	pageNumber,
			    	pageSize,
			    	keyword
			    });
	ctx.body = result;
});


//添加财务公司公司
router.post('/financeCompany/add',async(ctx,next) =>{
	let {
        company
    } = ctx.request.body;

    let result = await ctx.post('/web/company/create',{...company});
    ctx.body = result;
})

//财务公司详情页面
router.post('/financeCompany/detail',async(ctx,next) => {
    let{
        id
    }=ctx.request.body;

    let result = await ctx.get('/web/company/detail',{id});
    if (result.success && result.data && result.data.license) {
        result.data.license.map((item,index) =>{
            result.data.license[index].previewUrl = config('baseURL')+result.data.license[index].url;
        })
        
    }
    ctx.body = result;
})

//更新财务公司信息
router.post('/financeCompany/update',async(ctx,next) =>{
    let {
        company
    } = ctx.request.body;
    let result = await ctx.post('/web/company/update',{...company});

    ctx.body = result;
})

//删除财务公司
router.post('/financeCompany/delete',async(ctx,next) => {
    let{
        id_list
    }=ctx.request.body;

    let result = await ctx.post('/web/company/delete',{id_list});

    ctx.body = result;
})

//-------------财务公司相关接口End----------------



//-------------企业客户相关接口Start--------------
//获取母公司及终极母公司
router.post('/getParentCompanyList',async(ctx,next) => {
    let result = {
        parent:[],
        finalParent:[]
    };
    let parent = await ctx.get('/web/client/list-parent',{type:"PARENT"});
    let finalParent = await ctx.get('/web/client/list-parent',{type:"FINAL_PARENT"});


    if (parent.success && finalParent.success) {
        result.parent = parent.data;
        result.finalParent = finalParent.data;
    }

    ctx.body = result;
})


//企业客户列表
router.post('/companyCustom/list',async (ctx, next)=>{
    let {
        pageNumber=1,
        pageSize=10,
        keyword=""
    } = ctx.request.body;


    let result = await ctx.get('/web/client/list',{
                    pageNumber,
                    pageSize,
                    keyword
                });

    ctx.body = result;
});

//添加企业客户
router.post('/companyCustom/add',async(ctx,next) =>{
    let {
        custom
    } = ctx.request.body;

    let result = await ctx.post('/web/client/create',{...custom});
    ctx.body = result;
})

//获取企业客户详细信息
router.post('/companyCustom/detail',async(ctx,next) =>{
    let {
        id
    } = ctx.request.body;
    let result = await ctx.get('/web/client/detail',{id});
    if (result.success && result.data && result.data.license) {
        result.data.license.map((item,index) =>{
            result.data.license[index].previewUrl = config('baseURL')+result.data.license[index].url;
        })
        
    }
    if (result.success && result.data && result.data.equity) {
        result.data.equity.map((item,index) =>{
            result.data.equity[index].previewUrl = config('baseURL')+result.data.equity[index].url;
        })
        
    }
    ctx.body = result;
})

//更新企业客户信息
router.post('/companyCustom/update',async(ctx,next) =>{
    let {
        custom
    } = ctx.request.body;
    let result = await ctx.post('/web/client/update',{...custom});
    ctx.body = result;
})

//删除企业客户
router.post('/companyCustom/delete',async(ctx,next) =>{
    let {
        id_list
    } = ctx.request.body;
    let result = await ctx.post('/web/client/delete',{id_list});
    ctx.body = result;
})


//--------------企业账户相关接口End----------------



//--------------账户管理相关接口Start--------------
//账户管理列表
router.post('/accountManage/list',async (ctx, next)=>{
    let {
        pageNumber=1,
        pageSize=10,
        keyword=""
    } = ctx.request.body;


    let result = await ctx.get('/web/account/list',{
                    pageNumber,
                    pageSize,
                    keyword
                });
    ctx.body = result;
});

//添加账户
router.post('/accountManage/add',async(ctx,next) =>{
    let {
        account
    } = ctx.request.body;
    let result = await ctx.post('/web/account/create',{...account});
    ctx.body = result;
})

//获取账户详情
router.post('/accountManage/detail',async(ctx,next) =>{
    let {
        id
    } = ctx.request.body;
    let result = await ctx.get('/web/account/detail',{id});
    ctx.body = result;
})

//更新账户信息
router.post('/accountManage/update',async(ctx,next) =>{
    let {
        account
    } = ctx.request.body;
    let result = await ctx.post('/web/account/update',{...account});
    ctx.body = result;
})

//重置账户密码
router.post('/accountManage/resetPassword',async(ctx,next) =>{
    let{
        id,
        password,
    }=ctx.request.body;
    let result = await ctx.post('/web/account/reset-password',{id,password})
    ctx.body = result;
})

//修改账号状态
router.post('/accountManage/changeStatus',async(ctx,next) =>{
    let{
        active,
        id,
    }=ctx.request.body;
    let result = await ctx.post('/web/account/change-status',{active,id})
    ctx.body = result;
})

//删除账号信息
router.post('/accountManage/delete',async(ctx,next) =>{
    let{
        id_list,
    }=ctx.request.body;
    let result = await ctx.post('/web/account/delete',{id_list})
    ctx.body = result;
})
//---------------账户管理相关接口End-------------------


export default router;
